﻿Imports System.Data
Imports System.Data.SqlClient
Imports FreeTextBoxControls
Partial Class nnhomnhomfaq
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub



    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Dim blnAddNew As Boolean
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim intID As Int16
        If Not Page.IsPostBack Then
            hideText.Text = -1
            blnAddNew = True
            Button1.Text = "Thêm mới"
            If IsNumeric(Request.QueryString("id")) Then
                intID = Request.QueryString("id")
                BindGrid(intID)
                blnAddNew = False
                Button1.Text = "Cập nhật"
            End If
            Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
            Dim sSql As String = "select idnn,ngonngu from ngonngu"
            myConn.Open()
            Dim myAdapter As New SqlDataAdapter(sSql, myConn)
            Dim myDataSet2 As New DataSet
            myAdapter.Fill(myDataSet2, sSql)
            idnn.DataSource = myDataSet2
            idnn.DataBind()
            myConn.Close()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sSql As String
        If hideText.Text <= 0 Then
            sSql = "insert into dmnhomfaq (tennhomfaq,idnn,mota,TT,thutu,tukhoa,idvn) " & _
                    "values(@tennhomfaq,@idnn,@mota,@TT,@thutu,@tukhoa,@idvn)"
            blnAddNew = True
        Else
            sSql = "update dmnhomfaq set" & _
            " tennhomfaq=@tennhomfaq,idnn=@idnn" & _
            " ,TT=@TT,thutu=@thutu,mota=@mota,tukhoa=@tukhoa,idvn=@idvn" & _
            " where idnhomfaq=@idnhomfaq"
            blnAddNew = False
        End If
        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        Dim MyCommand As SqlCommand
        myConn.Open()
        MyCommand = New SqlCommand(sSql, myConn)
        If Not blnAddNew Then
            MyCommand.Parameters.Add(New SqlParameter("@idnhomfaq", hideText.Text))
        End If
        MyCommand.Parameters.Add(New SqlParameter("@tennhomfaq", tennhomfaq.Text))
        MyCommand.Parameters.Add(New SqlParameter("@TT", TT.Checked))
        MyCommand.Parameters.Add(New SqlParameter("@idnn", idnn.SelectedValue))
        MyCommand.Parameters.Add(New SqlParameter("@thutu", CInt(Thutu.Text)))
        MyCommand.Parameters.Add(New SqlParameter("@mota", mota.Text))
        MyCommand.Parameters.Add(New SqlParameter("@idvn", IIf(idvn.Text = "", 0, idvn.Text)))
        MyCommand.Parameters.Add(New SqlParameter("@tukhoa", txtTukhoa.Text))
        MyCommand.ExecuteNonQuery()
        myConn.Close()
        Response.Redirect("dmnhomfaq.aspx")


    End Sub
    Private Sub BindGrid(ByVal vId As Int16)
        On Error Resume Next
        Dim dr As DataRow
        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        Dim sSql As String = "select top 1 * from dmnhomfaq where idnhomfaq = " & vId
        myConn.Open()
        Dim myComm As New SqlCommand(sSql, myConn)
        Dim myDr As SqlDataReader
        myDr = myComm.ExecuteReader
        If myDr.Read Then
            tennhomfaq.Text = myDr("tennhomfaq")
            TT.Checked = myDr("TT")
            Thutu.Text = myDr("thutu")
            mota.Text = myDr("mota")
            idnn.SelectedValue = myDr("idnn")
            hideText.Text = myDr("idnhomfaq")
            txtTukhoa.Text = myDr("tukhoa")
            idvn.Text = myDr("idvn")
        End If
        myConn.Close()
    End Sub

End Class


